﻿using System.Data;
using WorkFlow.DBProxy;

namespace WorkFlow.Engine.NSActivity.NSRoutes
{
    public class WfParallelTypeRoute
    {
        /// <summary>
        ///     并行合并
        ///     前面执行的全部complete,执行后面的活动,否则不执行后续活动
        /// </summary>
        /// <param name="act"></param>
        public void AndJoin(WfActivity act)
        {
            string sql = string.Format(@"select f_id from t_activities t where  exists(
                                       select f_pre_activity from tr_paths p where t.f_r_act_id=p.f_pre_activity and f_after_activity =(
                                             select f_r_act_id from t_activities where f_id={0}
                                       )
                                ) and f_process_id={1}", act.ID, act.ProcessId);
            DataTable dt = DB.MapToDataTable(string.Format(sql, act.ID));
            if (dt == null || dt.Rows.Count == 0) return;

            //如果前置活动全部为完成
            bool ident = true;
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                if (int.Parse(dt.Rows[i]["F_STATUS"].ToString().Trim()) == WfFieldCode.DbCompleteState) continue;
                ident = false;
                break;
            }
            if (!ident)
            {
                act.Ready(-1);
                return;
            }

            //如果前面的全部执行完成者执行后续的活动
            if (act.Status != WfFieldCode.DbReadyState) act.Ready(-1);
            act.Run();
            act.Complete();
        }

        /// <summary>
        ///     同时激活后续的活动(并行)
        /// </summary>
        /// <param name="act"></param>
        /// <param name="activatorID"></param>
        /// <param name="inputValue"></param>
        public void AndSplit(WfActivity act ,int activatorID, int? inputValue)
        {
            int curActId = act.ID;
            int curProcId = act.ProcessId;

            if (act.Status != WfFieldCode.DbReadyState) act.Ready(activatorID);
            act.Run();
            act.Complete();
        }
    }
}